home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / y2l.lha / y2l / Tables.c < prev    next >
C/C++ Source or Header  |  1992-08-20  |  11KB  |  573 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "Memory.h"
  4. #include "StringMem.h"
  5. #include "Tables.h"
  6.  
  7. /* types for nodes of linked lists */
  8. typedef struct tName {
  9.         tStringRef name;
  10.         tStringRef alias;
  11.         struct tName *cdr;
  12.            };
  13.  
  14. typedef struct tToken {
  15.         tStringRef name;
  16.         int value;
  17.         struct tToken *cdr;
  18.            };
  19.  
  20. typedef struct tType {
  21.         tStringRef name;
  22.         tStringRef type;
  23.         struct tType *cdr;
  24.            };
  25.  
  26. typedef struct tRule {
  27.         int tag; /* 0 = text, 1 = rule */
  28.         union {
  29.             struct tCell * text;
  30.             struct tElmt * rule;
  31.               } car;
  32.         struct tRule *cdr;
  33.            };
  34.  
  35. /* heads of linked lists */
  36. struct tName * name_list = 0;
  37. struct tToken * token_list = 0;
  38.  
  39. struct tType * type_list = 0;
  40. struct tCell * oper_list = 0;
  41. struct tCell * global_list = 0;
  42. struct tCell * action_list;
  43.  
  44. tStringRef start_symbol = 0;
  45.  
  46. struct tElmt * rule_elmts_list;
  47. struct tCell * text_list = 0;
  48. struct tRule * rule_list = 0;
  49.  
  50. int token_val;
  51. int type = 0;
  52. int generate_actions = 1;
  53.  
  54. bool MyEqual (r, s)
  55. tStringRef r;
  56. tString s;
  57. {
  58.   return LengthSt (r) == strlen (s) && IsEqualSt (r, s);
  59. }
  60.  
  61. void print_symbol (r)
  62. tStringRef r;
  63. {
  64.   register struct tName * p;
  65.   char v[256];
  66.  
  67.   StGetString (r, v);
  68.   if (v[0] == '\'') {
  69.     (void) printf ("%s", v);
  70.     return;
  71.   }
  72.   for (p = name_list; p; p = p->cdr)
  73.     if (MyEqual (p->name, v)) {
  74.       WriteString (stdout, p->alias);
  75.       return;
  76.     }
  77.   (void) printf ("%s", v);
  78. }
  79.  
  80. /* check if name is already in name_list */
  81. int unique_name (v)
  82. char * v;
  83. {
  84.   register struct tName * p;
  85.   for (p = name_list; p; p = p->cdr)
  86.     if (p->alias && MyEqual (p->alias, v)) return 0;
  87.   return 1;
  88. }
  89.  
  90. /* if name not yet in list, append it */
  91. void put_name (name)
  92. tStringRef name;
  93. {
  94.   register struct tName * p;
  95.   char s[256];
  96.  
  97.   for (p = name_list; p; p = p->cdr) {
  98.     StGetString (name, s);
  99.     if (MyEqual (p->name, s)) return;
  100.   }
  101.   p = (struct tName *) Alloc (sizeof (struct tName));
  102.   p->name  = name;
  103.   p->alias = 0;
  104.   p->cdr   = name_list;
  105.   name_list = p;
  106. }
  107.  
  108. /* transform every ident with dots */
  109. void process_names ()
  110. {
  111.   register struct tName * p;
  112.   register int i;
  113.   int dotflag;
  114.   char s[256];
  115.   char v[256];
  116.  
  117.   for (p = name_list; p; p= p->cdr) {
  118.     StGetString (p->name, s);
  119.     if (s[0] != '\'') {
  120.       dotflag = 0;
  121.       for (i = 0; s[i]; i++) 
  122.         if (s[i] == '.') {
  123.         dotflag = 1;
  124.       s[i] = '_';
  125.         }
  126.       if (dotflag) {
  127.         i = 0;
  128.         do {
  129.       i++;
  130.       (void) sprintf (v, "%s_%d", s, i);
  131.         } while (! unique_name (v));
  132.         p->alias = PutString (v, strlen (v));
  133.       } else
  134.         p->alias = p->name;
  135.     } else
  136.       p->alias = p->name;
  137.   }
  138. }
  139.  
  140. /* if token not yet in list, append it */
  141. void put_token (name, value)
  142. tStringRef name;
  143. int    value;
  144. {
  145.   register struct tToken * p;
  146.   char s[256];
  147.  
  148.   for (p = token_list; p; p = p->cdr) {
  149.     StGetString (name, s);
  150.     if (MyEqual (p->name, s)) return;
  151.   }
  152.   p = (struct tToken *) Alloc (sizeof (struct tToken));
  153.   p->name  = name;
  154.   p->value = value;
  155.   p->cdr   = token_list;
  156.   token_list = p;
  157. }
  158.  
  159. /* print the TOKEN section */
  160. void aux_print_tokens (p)
  161. struct tToken * p;
  162. {
  163.   register int i, v;
  164.   char s[256];
  165.  
  166.   if (p) {
  167.     aux_print_tokens (p->cdr);
  168.     (void) printf ("\t");
  169.     print_symbol (p->name);
  170.     if (p->value) {
  171.       v = p->value;
  172.     } else {
  173.       StGetString (p->name, s);
  174.       if (s[0] == '\'') {
  175.     if (s[1] == '\\') {
  176.       switch (s[2]) {
  177.         case 'b' : v = 8; break;
  178.         case 't' : v = 9; break;
  179.         case 'n' : v = 10; break;
  180.         case 'f' : v = 12; break;
  181.         case 'r' : v = 13; break;
  182.         default :
  183.           if ((s[2] >= '0') && (s[2] <= '7')) {
  184.         v = 0;
  185.         for (i = 2; s[i] != '\''; i++)
  186.           v = 8 * v + s[i] - '0';
  187.           } else
  188.         v = (int) s[2];
  189.           break;
  190.       }
  191.     } else
  192.       if ((s[1] == '\'') && (s[2] == '\'') && (s[4] =='\0')) {
  193.         v = (int) '\'';
  194.       } else
  195.         if (s[3]) {
  196.           v = token_val++;
  197.         } else
  198.           v = (int) s[1];
  199.       } else
  200.     v = token_val++;
  201.     }
  202.     if (v)
  203.       (void) printf ("\t= %d", (int) v);
  204.     (void) printf ("\n");
  205.   }
  206. }
  207.  
  208. void print_tokens ()
  209. {
  210.   token_val = 257;
  211.   (void) printf ("\nTOKEN\n");
  212.   aux_print_tokens (token_list);
  213.   (void) printf ("\terror\n");
  214. }
  215.  
  216. /* put type of symbol to list */
  217. void put_type (name, type)
  218. tStringRef name;
  219. tStringRef type;
  220. {
  221.   struct tType *p;
  222.  
  223.   p = (struct tType *) Alloc (sizeof (struct tType));
  224.   p->name = name;
  225.   p->type = type;
  226.   p->cdr  = type_list;
  227.   type_list = p;
  228. }
  229.  
  230. /* get type of symbol */
  231. tStringRef search_type (t)
  232. tStringRef t;
  233. {
  234.   char s[256];
  235.   register struct tType * p;
  236.  
  237.   StGetString (t, s);
  238.   for (p = type_list; p; p = p->cdr)
  239.     if (MyEqual (p->name, s)) return p->type;
  240.   return 0;
  241. }
  242.  
  243. tStringRef aux_get_type (p, n)
  244. struct tElmt * p;
  245. int * n;
  246. {
  247.   if (p->tag != 2 /* lhs */) {
  248.     tStringRef t = aux_get_type (p->cdr, n);
  249.     if (p->tag == 0 /* symbol */ || p->tag == 1 /* action */ || p->tag == 4 /* last action */) -- * n;
  250.     if (! * n) {
  251.       return search_type (p->car.symbol);
  252.     } else {
  253.       return t;
  254.     }
  255.   } else {
  256.     return 0;
  257.   }
  258. }
  259.  
  260. tStringRef aux_get_type_0 ()
  261. {
  262.   register struct tElmt * p;
  263.  
  264.   for (p = rule_elmts_list; p->cdr; p = p->cdr);
  265.   return search_type (p->car.symbol);
  266. }
  267.  
  268. /* if n = 0 get type of lhs, else of n-th rhs item, 0 if no type */
  269. tStringRef get_type (n)
  270. int n;
  271.   if (n < 0) {
  272.     return 0;
  273.   } else if (! n) {
  274.     return aux_get_type_0 ();
  275.   } else
  276.     return aux_get_type (rule_elmts_list, &n);
  277. }
  278.  
  279. /* put operand to list */
  280. void put_oper (s)
  281. tStringRef s;
  282. {
  283.   struct tCell * p;
  284.  
  285.   p = (struct tCell *) Alloc (sizeof (struct tCell));
  286.   p->car = s;
  287.   p->cdr = oper_list;
  288.   oper_list = p;
  289. }
  290.  
  291. /* print OPER section */
  292. void aux_print_opers (p)
  293. struct tCell * p;
  294. {
  295.   if (p) {
  296.     aux_print_opers (p->cdr);
  297.     print_symbol (p->car);
  298.     (void) printf ("\t");
  299.   }
  300. }
  301.  
  302. void print_opers ()
  303. {
  304.   if (oper_list) {
  305.     (void) printf ("\nOPER");
  306.     aux_print_opers (oper_list);
  307.     (void) printf ("\n");
  308.   }
  309. }
  310.  
  311. /* put text to GLOBAL section */
  312. void put_global (v)
  313. tStringRef v;
  314. {
  315.   struct tCell * p;
  316.  
  317.   p = (struct tCell *) Alloc (sizeof (struct tCell));
  318.   p->car = v;
  319.   p->cdr = global_list;
  320.   global_list = p;
  321. }
  322.  
  323. char * YACC1 [] = {
  324.    "\n",
  325.    "# define tParsAttribute    YYSTYPE\n",
  326.    "# define yySynAttribute    yyval\n",
  327.    "# define yyTerminal    yychar\n",
  328.    "# define yyclearin\n",
  329.    "# define yyerrok\n",
  330.    "# ifndef YYMAXDEPTH\n",
  331.    "# define YYMAXDEPTH    150\n",
  332.    "# endif\n",
  333.    0
  334. };
  335.  
  336. char * YACC2 [] = {
  337.    "# ifndef YYSTYPE\n",
  338.    "# define YYSTYPE    int\n",
  339.    "# endif\n",
  340.    0
  341. };
  342.  
  343. char * YACC3 [] = {
  344.    "YYSTYPE yylval;\n",
  345.    "# define yylvalDef\n",
  346.    "# define YYERROR\n",
  347.    "# define YYACCEPT    ReleaseArray (& yyStateStack, & yyStateStackSize, sizeof (yyStateRange)); \\\\\n",
  348.    "            ReleaseArray (& yyAttributeStack, & yyAttrStackSize, sizeof (tParsAttribute)); \\\\\n",
  349.    "            return(0)\n",
  350.    "# define YYABORT    ReleaseArray (& yyStateStack, & yyStateStackSize, sizeof (yyStateRange)); \\\\\n",
  351.    "            ReleaseArray (& yyAttributeStack, & yyAttrStackSize, sizeof (tParsAttribute)); \\\\\n",
  352.    "            return(1)\n",
  353.    "# define YYBACKUP(newtoken, newvalue)    YYERROR\n",
  354.    "# define YYRECOVERING()    yyIsRepairing\n",
  355.    "\n",
  356.    0
  357. };
  358.  
  359. void yacc_globals ()
  360. {
  361.    register int i;
  362.  
  363.    for (i = 0; YACC1 [i]; i++)
  364.       put_global (PutString (YACC1 [i], strlen (YACC1 [i])));
  365.    if (! type) for (i = 0; YACC2 [i]; i++)
  366.       put_global (PutString (YACC2 [i], strlen (YACC2 [i])));
  367.    for (i = 0; YACC3 [i]; i++)
  368.       put_global (PutString (YACC3 [i], strlen (YACC3 [i])));
  369. }
  370.  
  371. /* print GLOBAL section */
  372. void aux_print_global (p)
  373. struct tCell * p;
  374. {
  375.   if (p) {
  376.     aux_print_global (p->cdr);
  377.     WriteString (stdout, p->car);
  378.   }
  379. }
  380.  
  381. void print_global ()
  382. {
  383.   if (global_list) {
  384.     (void) printf ("EXPORT {\n");
  385.     (void) printf ("# define yacc_interface\n");
  386.     (void) printf ("# define lex_interface\n");
  387.     (void) printf ("}\n\n");
  388.     (void) printf ("GLOBAL {\n");
  389.     aux_print_global (global_list);
  390.     (void) printf ("}\n");
  391.   }
  392. }
  393.  
  394. /* put text to action */
  395. void put_action (v)
  396. tStringRef v;
  397. {
  398.   struct tCell * p;
  399.  
  400.   p = (struct tCell *) Alloc (sizeof (struct tCell));
  401.   p->car = v;
  402.   p->cdr = action_list;
  403.   action_list = p;
  404. }
  405.  
  406. /* put action list to rule */
  407. void put_whole_action (v, b)
  408. struct tCell  * v;
  409. bool b;
  410. {
  411.   struct tElmt * p;
  412.  
  413.   p = (struct tElmt *) Alloc (sizeof (struct tElmt));
  414.   p->tag = b ? 4 : 1;
  415.   p->car.action = v;
  416.   p->cdr = rule_elmts_list;
  417.   rule_elmts_list = p;
  418. }
  419.  
  420. /* put lhs to rule */
  421. void put_lhs (v)
  422. tStringRef v;
  423. {
  424.   struct tElmt * p;
  425.  
  426.   p = (struct tElmt *) Alloc (sizeof (struct tElmt));
  427.   p->tag = 2;
  428.   p->car.symbol = v;
  429.   p->cdr = rule_elmts_list;
  430.   rule_elmts_list = p;
  431. }
  432.  
  433. /* put symbol to rule */
  434. void put_symbol (v)
  435. tStringRef v;
  436. {
  437.   struct tElmt * p;
  438.  
  439.   p = (struct tElmt *) Alloc (sizeof (struct tElmt));
  440.   p->tag = 0;
  441.   p->car.symbol = v;
  442.   p->cdr = rule_elmts_list;
  443.   rule_elmts_list = p;
  444. }
  445.  
  446. /* put delimiter to rule */
  447. void put_delim (v)
  448. tStringRef v;
  449. {
  450.   struct tElmt * p;
  451.  
  452.   p = (struct tElmt *) Alloc (sizeof (struct tElmt));
  453.   p->tag = 3;
  454.   p->car.symbol = v;
  455.   p->cdr = rule_elmts_list;
  456.   rule_elmts_list = p;
  457. }
  458.  
  459. /* put rule to RULE section */
  460. void put_rule (v)
  461. struct tElmt * v;
  462. {
  463.   struct tRule * p;
  464.  
  465.   p = (struct tRule *) Alloc (sizeof (struct tRule));
  466.   p->tag = 1;
  467.   p->car.rule = v;
  468.   p->cdr = rule_list;
  469.   rule_list = p;
  470. }
  471.  
  472. /* put text to list */
  473. void append_text (v)
  474. tStringRef v;
  475. {
  476.   struct tCell * p;
  477.  
  478.   p = (struct tCell *) Alloc (sizeof (struct tCell));
  479.   p->car = v;
  480.   p->cdr = text_list;
  481.   text_list = p;
  482. }
  483.  
  484. /* put text list to rule */
  485. void put_text (v)
  486. struct tCell * v;
  487. {
  488.   struct tRule * p;
  489.  
  490.   if (v) {
  491.     p = (struct tRule *) Alloc (sizeof (struct tRule));
  492.     p->tag = 0;
  493.     p->car.text = v;
  494.     p->cdr = rule_list;
  495.     rule_list = p;
  496.     text_list = 0;
  497.   }
  498. }
  499.  
  500. /* print RULE section */
  501. void print_actions (p)
  502. struct tCell * p;
  503. {
  504.   if (p) {
  505.     print_actions (p->cdr);
  506.     WriteString (stdout, p->car);
  507.   }
  508. }
  509.  
  510. void print_rule_elmts (p)
  511. struct tElmt * p;
  512. {
  513.   if (p) {
  514.     print_rule_elmts (p->cdr);
  515.     switch (p->tag) {
  516.       case 1: /* action    */
  517.     if (generate_actions) {
  518.        (void) printf ("{{");
  519.        print_actions (p->car.action);
  520.        (void) printf (" ");
  521.     }
  522.     break;
  523.       case 4: /* last action */
  524.     if (generate_actions) {
  525.        (void) printf ("{ $$ = $1; {");
  526.        print_actions (p->car.action);
  527.        (void) printf (" ");
  528.     }
  529.     break;
  530.       case 3: /* delim    */
  531.       case 0: /* symbol    */
  532.     print_symbol (p->car.symbol); (void) printf (" ");
  533.     break;
  534.       default: /* lhs    */
  535.     ;
  536.     }
  537.   }
  538. }
  539.  
  540. void print_text (p)
  541. struct tCell * p;
  542. {
  543.   if (p) {
  544.     print_text (p->cdr);
  545.     WriteString (stdout, p->car);
  546.   }
  547. }
  548.  
  549. void aux_print_rules (p)
  550. struct tRule * p;
  551. {
  552.   if (p) {
  553.     aux_print_rules (p->cdr);
  554.     if (p->tag) {
  555.       print_rule_elmts (p->car.rule);
  556.     } else
  557.       print_text (p->car.text);
  558.   }
  559. }
  560.  
  561. void print_rules ()
  562. {
  563.   (void) printf ("\nRULE\n");
  564.   if (start_symbol) {
  565.     (void) printf ("\n__START__\t: ");
  566.     print_symbol (start_symbol);
  567.     (void) printf (" .");
  568.   }
  569.   aux_print_rules (rule_list);
  570.   (void) printf ("\n");
  571. }
  572.